<?php
session_start();
include "dbconnect.php";
$courseerror=$gradeerror="";
$courseid=$grade="";
$post=0;
if(!isset($_SESSION['Username']))
{
  header("location:index.php");

}
else 
{
	if($_SESSION['Type']!=3)
  header("location:unauthorized.php");
	//echo isset($_SESSION['Username']);
	
}
$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
mysql_select_db($_DB_NAME,$con);
$query=0;
	if(isset($_REQUEST['openingid']))
			{
				$openingID= $_REQUEST["openingid"];
				$userName=$_SESSION["Username"];
				$_SESSION["openingid"]=$openingID;
				mysql_query("SET @openingid=".$openingID);
				$Openingdetails =	mysql_query(" select o.*,c.*,s.Firstname,s.Lastname from openings o,courses c,users s where openingid=@openingid and o.courseid=c.courseid and s.username=o.instructor" );
				if($Openingdetails==false)
				{
					die(mysql_error());
				}
					$query=1;
			}
			else if($_SERVER["REQUEST_METHOD"] != "POST")
			{
				header("location:applyview.php");
			}


	if($_SERVER["REQUEST_METHOD"] == "POST")
	{
					if(isset($_SESSION["openingid"]) and !isset($_REQUEST['openingid']))
					{
						mysql_query("SET @openingid=".$_SESSION["openingid"]);
							$Openingdetails =	
						mysql_query(" select o.*,c.*,s.Firstname,s.Lastname from openings o,courses c,users s where openingid=@openingid and o.courseid=c.courseid and 	s.username=o.instructor" );
		
					$query=1;
					}
				$openingID=$_SESSION["openingid"];
				$userName=$_SESSION["Username"];
				$courseid=$_POST["course"];
				$grade=$_POST["grade"];
			if(empty($courseid))
				{
				$courseerror="Provide a course";
				}
			if(empty($grade))
				{
				$gradeerror="Fill in the grade";
				}
				if($gradeerror=="" and $courseerror=="")
				{
					$insertdetails =	mysql_query(" insert into appply (grade,openingid,username,courseid) values ('$grade',$openingID,'$userName','$courseid')" 	);
					if($insertdetails == false)
					{
					die(mysql_error());
					}
					else
					{
					if(isset($_SESSION["openingid"]))
					unset($_SESSION["openingid"]);
					echo "<script>alert(\"Applied succesfully!\")</script>";
					echo "<script>location.href = 'applyview.php?apply=1'</script>";
					}
				
				}

//+"and year like '%"+$postyear+"%' order by year desc");
		mysql_close($con);
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />


<title>View</title>

<script src="media/js/jquery.js" type="text/javascript"></script>
<script src="media/js/jquery.datatables.js" type="text/javascript"></script>

  	<script type="text/javascript" src="scripts/jquery-2.0.3.min.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="jqwidgets/jqxexpander.js"></script>
   
	</script>

 <link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />
 <link rel="stylesheet" href="jqwidgets/styles/jqx.summer.css" type="text/css" />
  
 <script type="text/javascript">
        $(document).ready(function () {
            // Create jqxExpander
            $("#opendetails").jqxExpander({});
			$('#userInfo').jqxExpander({});
        });
		
	</script>

<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
    $('#datatables').dataTable(
	{	"bJQueryUI":true
		});
});
</script>

<style type="text/css">
@import "media/css/demo_table_jui.css";
@import "media/themes/smoothness/jquery-ui-1.8.4.custom.css";
</style>

</head>

<body style="min-width:1280px">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img alt="Indiana University" height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="grey">
</div>
<center>
			<div id="hnav" style="background:none"><!--system-region name="GLOBAL NAVIGATION"/-->
              <ul>
            
                 <li style="width:200px;" ><a href="applyview.php">Apply</a></li>
                <li style="width:200px;"><a href="profile.php">Profile</a></li>
               
              </ul>
      	</div>
        </center>
         <span style="width:100px;margin-left:300px;">
                <button class="hyperbutton" onclick="location.href = 'logout.php'" >logout</button>
         </span>
            <div class="footbody">
           
            <div id="opendetails">
            <div>
            <?php 
                if($query==1)
                {
             while($row=mysql_fetch_array($Openingdetails))
             {	 if($row['description']== NULL ) $description= $row['courseid']; else  $description= $row['description'];
                 echo "Opening details for ".$description;
             ?>
             </div>
             <div>
             <table class="registrationtable" cellpadding="5" cellspacing="5">
             <tbody>
             <tr>
                 <td>
                 <label >Course</label>
                 </td>
                 <td>
                 <label  class="crimson-text"><?php if($row['description']== NULL ) echo $row['courseid']; else  echo $row['description'];?></label>
                 </td>
                 <td>
                 <label>Insrtuctor</label>
                 </td>
                 <td>
                 <label  class="crimson-text"><?php echo $row['Firstname'].$row['Lastname']?></label>
                 </td>
             </tr>
             <tr>
                 <td>
                 <label>Total</label>
                 </td>
                 <td
                 <label  class="crimson-text"><?php echo $row['Total']?></label>
                 </td>
                 <td>
                 <label >Filled</label>
                 </td>
                 <td>
                 <label  class="crimson-text"><?php echo $row["Filled"]?></label>
                 </td>
             </tr>
             <tr>
                 <td>
                 <label >Location</label>
                 </td>
                 <td>
                 <Textarea  disabled class="crimson-text"><?php echo $row["location"]?></textarea>
                 </td>
                 <td>
                 <label >Days</label>
                 </td>
                 <td>
                 <Textarea disabled="disabled"  class="crimson-text"><?php echo $row["Day"]?></textarea>
                 </td>
             </tr>
              <tr>
                 <td>
                 <label >Timings</label>
                 </td>
                 <td>
               <Textarea disabled="disabled"  class="crimson-text"><?php echo $row["Timing"]?></textarea>
                 </td>
               
             </tr>
            </tbody>
             </table>
             <?php }}?>
             </div>
         </div>
                    <div id="userInfo" style="margin-top:20px">
                        <div>
                        Fill in details
                        </div>
                        <div>
                        <form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" >
                        <table class="registrationtable">
                        <tbody>
                        <tr>
                        <td>
                        <label class="crimson-text">Course</label> 
                        </td>
                        <td>
                        <input type="text" name="course"  class="crimson-border" value="<?php echo $courseid?>"/>
                        </td>
                        <td class="error" style="width:150px;"><?php echo $courseerror?>
                        </td>
                        <td style="width:50px;">
                        <td>
                        <label class="crimson-text">Grade</label> 
                        </td>
                        <td>
                        <input type="text" name="grade"  class="crimson-border"   value="<?php echo $grade?>" />
                        </td>
                         <td class="error" style="width:150px;"><?php echo $gradeerror?>
                        </td>
                        </tr>
                        <tr/>
                         <tr/>
                          <tr/>
                           <tr/><tr/>
                         <tr/>
                          <tr/>
                           <tr/>
                        <tr>
                        <td>
                        <input type="submit"  value="Apply"/>
                        </td>
                        </tr>
                        </form>
                        </tbody>
                        </table>
                        </div>
           </div>
           </div>
</body>
</html>